package huv.andlogin;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;




import android.os.Environment;


@SuppressWarnings("serial")
public class CfgParser extends HashMap<String,Cfg> {
	public CfgParser() throws IOException{
		 File folder = new File(Environment.getExternalStorageDirectory()+"/andlogin");
		 if(!(folder.exists() && folder.isDirectory())){
			 folder.mkdir();			 
		 }
		 File[] cfgFile = folder.listFiles();
		 for(int i=0; i< cfgFile.length && cfgFile[i].getName().contains(".cfg"); i++){
			 FileReader fw = new FileReader(cfgFile[i]);
		     BufferedReader fb = new BufferedReader(fw);
		     Cfg cfgTemp = new Cfg("","");
		     String s;
		     Boolean property=false;
		     Boolean request = false;

		     while((s = fb.readLine()) != null){
		    	if(!property && s.startsWith("[name]")){
		    		property = false;
		    		request=false;
		    		s=fb.readLine();
		    		cfgTemp.setName(s);
		    	}else if(!property && s.startsWith("[url]")){
		    		s=fb.readLine();
		    		cfgTemp.setUrl(s);
		    	}else if(!property &&  s.startsWith("[essid]")){
		    		property = false;
		    		request=false;
		    		s=fb.readLine();
		    		cfgTemp.setEssid(s);
		    	}else if(s.startsWith("[RequestProperty]")){
		    		property = true;
		    		request = false;
		    	}else if(s.startsWith("[PostRequest]")){
		    		request=true;
		    		property=false;
		    	}else if(property && s.length()>1){
		    		request=false;
		    		String[] v = s.split(":");
		    		cfgTemp.addRequestProperty(v[0],v[1]);
		    	}else if(request && s.length()>1){
		    		property=false;
		    		String[] v;
		    		v =s.split(":");
		    		if(v.length==2)
		    			cfgTemp.addPostRequest(v[0],v[1]);
		    		else
		    			cfgTemp.addPostRequest(v[0],"");
		    	}
		     }
		     fb.close();
		     fw.close();
		     this.put(cfgTemp.geName(), cfgTemp);
		 
		 }
	}
	public List<HashMap<String,String>>  listCfg(){
		 List<HashMap<String,String>> l = new  ArrayList<HashMap<String,String>>();
		Iterator<Entry<String, Cfg>> i = this.entrySet().iterator();
		while(i.hasNext()){
			HashMap<String,String> t = new HashMap<String,String>();
			t.put("name", i.next().getKey());
			l.add(t);
		}
		return l;
	}	
	public Cfg findSSID(String ssid){
		Iterator<Entry<String, Cfg>> i = this.entrySet().iterator();
		while(i.hasNext()){
			Entry<String, Cfg> t =i.next();
			Cfg tc = t.getValue();
			String c_ssid = tc.getEssid();
			if(ssid.equals(c_ssid))
				return t.getValue();
		}
		return null;
	}
public void saveState(){
		Iterator<Entry<String, Cfg>> ic = this.entrySet().iterator();
		while(ic.hasNext()){
			Entry<String,Cfg> t = ic.next();
			Cfg c = t.getValue();
			File file = new File(Environment.getExternalStorageDirectory()+"/andlogin/"+c.geName()+".cfg");
			FileWriter fw=null;
			try {
				fw = new FileWriter(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			BufferedWriter fb = new BufferedWriter(fw);
			
			try {
				fb.write("[name]\n");
				fb.write(c.geName());
				fb.write("\n[url]\n");
				fb.write(c.getUrl());
				fb.write("\n[essid]\n");
				fb.write(c.getEssid());
				fb.write("\n[RequestProperty]\n");
				@SuppressWarnings("unchecked")
				Iterator<Entry<String, String>> ir = (Iterator<Entry<String,String>>) c.getRPIterator();
				while(ir.hasNext()){
					Entry<String,String> tr = ir.next();
					fb.write(tr.getKey()+":"+tr.getValue()+"\n");
					
				}
				fb.write("\n[PostRequest]\n");
				@SuppressWarnings("unchecked")
				Iterator<Entry<String,String>> ip =(Iterator<Entry<String,String>>) c.getPRIterator();
				while(ip.hasNext()){
					Entry<String,String> tp = ip.next();
					fb.write(tp.getKey()+":"+tp.getValue()+"\n");
					
				}
				fb.close();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
}	
	


